package com.shanghaionstar.user.presenter;

import android.content.Context;

import com.onstar.cn.auth.userProfile.Vehicle;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.HeadImgRes;
import com.shanghaionstar.sqlite.BroadcastHeaderService;
import com.shanghaionstar.user.iview.IMoreView;
import com.shanghaionstar.user.service.MoreService;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.FinalConfigParams;

import rx.functions.Action1;

/**
 * Created by 7828wc on 16/7/13.
 */
public class MorePresenter extends BasePresenter<HeadImgRes> {

    private MoreService service;
    private IMoreView mMoreView;

    public MorePresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<HeadImgRes> view) {
        super.setView(view);
        service = new MoreService(view.onActivity());
        this.setServiceProvider(service);
        mMoreView = (IMoreView) view;
    }

    public void broadCastClick() {
        Context baseActivity = view.onActivity();
//        BusinessUtil.sendReportByClickType(baseActivity, FunctionNameFindId.SliderNotice.getStringValue());
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        String curIdStr = null;
        if (vehicle != null) {
            curIdStr = new BroadcastHeaderService(baseActivity).getLastBroadcastID(vehicle.getVin());
        }
        if (!CommonUtil.isNull(curIdStr)) {
            long curId = Long.parseLong(curIdStr);
            PreferencesUtls.getInstance(baseActivity).putLong(FinalConfigParams.BROADCAST_LAST_ID, curId);
            PreferencesUtls.getInstance(baseActivity).putString(FinalConfigParams.BROADCAST_UNREAD,
                    FinalConfigParams.FALSE);
        }
    }

    public void getHeadImg() {
        subscription = service.getHeadImg().subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<HeadImgRes>() {
                    @Override
                    public void call(HeadImgRes res) {
                        if (mMoreView != null && view.onActivity() != null)
                            mMoreView.onReponseSuccess(res);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (mMoreView != null && view.onActivity() != null)
                            mMoreView.getHeadError();
                    }
                });
    }

}
